package de.playmee;

import de.playmee.R;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;

public class PreferencesActivity extends PreferenceActivity {
	
	private ImageManager imageManager;
	private String busy = "";
	private Preference clearCachePreference;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    addPreferencesFromResource(R.xml.preferences);
	    
	    // TODO change username and/or password
	    
	    imageManager = ((Playmee) getApplication()).getImageManager();
	    try {
	    	busy = getResources().getString(R.string.preferences_cache) + " ";
	    } catch (NotFoundException nfe) {
	    	busy = "";
	    }
	    
	    clearCachePreference = findPreference("cache");
	    clearCachePreference.setOnPreferenceClickListener(new OnPreferenceClickListener() {
			@Override
			public boolean onPreferenceClick(Preference preference) {
				imageManager.clearCacheDir();
				refreshData();
				return false;
			}
		});
	    refreshData();
	}
	
	@Override
	protected void onRestart() {
		super.onRestart();
		refreshData();
	}
	
	private void refreshData() {
		// refresh size of the cache folder
		long cacheSize = imageManager.getCacheDirSize();
		if (cacheSize < 1000) {
			clearCachePreference.setSummary(busy + cacheSize + " Bytes");
			return;
		}
		cacheSize /= 1000;
		clearCachePreference.setSummary(busy + cacheSize + " KB");
	}
}